//Copyright (C) 1998-2008, Sumisho Computer Systems Corp. All Rights Reserved.

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

/**
 * @author Hitoshi Okada
 */

package com.curlap.orb.type;

import java.util.List;
import java.util.ArrayList;
//import java.lang.reflect.Method;
//import java.lang.reflect.Modifier;
//import java.lang.reflect.InvocationTargetException;

//import com.curlap.orb.util.ReflectionUtil;

/**
 * SerializableRecordSet
 */
public class SerializableRecordSet implements java.io.Serializable 
{
	private static final long serialVersionUID = 1L;

    private SerializableRecordField[] fields;
    private List<Object> records;

    // constructor
    public SerializableRecordSet(SerializableRecordField[] fields)
    {
        this.fields = fields;
        records = new ArrayList<Object>();
    }

    // constructor 
    //   NOTE: Generate SerializableRecordField[] from Class's getters.
    /*   PROBLEMS: When we use Class.getDeclaredMethod, method's orders are wrong.
    public SerializableRecordSet(Class type)
    {
        List<SerializableRecordField> fields = new ArrayList<SerializableRecordField>();
        Method[] methods = type.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++)
        {
            Method method = methods[i];
            System.out.println("#####"+method.getName());
            if (!Modifier.isPublic(method.getModifiers()))
                continue;
            if (ReflectionUtil.isGetter(method))
                fields.add(new SerializableRecordField(ReflectionUtil.toFieldName(method.getName()), method.getReturnType()));
        }
        this.fields = fields.toArray(new SerializableRecordField[fields.size()]);
        records = new ArrayList<Object>();
    }
    */

    public SerializableRecordField[] getFields(){
        return fields;
    }

    // add one record from column data's array
    public void addRecord(Object[] record)
    {
        records.add(record);
    }

    // add one record from Object's getter
    /* PROBLEM: see this.constructor
    public void addRecord(Object record) throws DataTypeException
    {
        Object[] values = new Object[fields.length];
        Method[] methods = record.getClass().getDeclaredMethods();
        int valueNumber = 0;
        for (int i = 0; i < methods.length; i++)
        {
            Method method = methods[i];
            if (!Modifier.isPublic(method.getModifiers()))
                continue;
            if (ReflectionUtil.isGetter(method))
            {
                try
                {
                    values[valueNumber++] = method.invoke(record);
                    if (valueNumber >= fields.length)
                        break;
                }
                catch (InvocationTargetException e)
                {
                    throw new DataTypeException(e);
                }
                catch (IllegalAccessException e)
                {
                    throw new DataTypeException(e);
                }
            }
        }
        addRecord(values);
    }
    */

    public void clearRecords()
    {
        records.clear();
    }

    public Object getRecord(int index)
    {
        return records.get(index);
    }

    public int getRecordSize()
    {
        return records.size();
    }
}
